package org.basic.mail.service.impl;

import org.basic.common.core.utils.MapstructUtils;
import org.basic.common.mybatis.core.page.TableDataInfo;
import org.basic.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.basic.mail.domain.SysMailLog;
import org.basic.mail.domain.bo.SysMailLogBo;
import org.basic.mail.domain.vo.SysMailLogVo;
import org.basic.mail.mapper.SysMailLogMapper;
import org.basic.mail.service.ISysMailLogService;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 邮件日志Service业务层处理
 *
 * @author Shao Hua
 * @date 2023-07-28
 */
@RequiredArgsConstructor
@Service
public class SysMailLogServiceImpl implements ISysMailLogService {

    private final SysMailLogMapper baseMapper;

    /**
     * 查询邮件日志
     */
    @Override
    public SysMailLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询邮件日志列表
     */
    @Override
    public TableDataInfo<SysMailLogVo> queryPageList(SysMailLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysMailLog> lqw = buildQueryWrapper(bo);
        Page<SysMailLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询邮件日志列表
     */
    @Override
    public List<SysMailLogVo> queryList(SysMailLogBo bo) {
        LambdaQueryWrapper<SysMailLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysMailLog> buildQueryWrapper(SysMailLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysMailLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAccountId() != null, SysMailLog::getAccountId, bo.getAccountId());
        lqw.eq(bo.getTemplateId() != null, SysMailLog::getTemplateId, bo.getTemplateId());
        lqw.eq(bo.getSendStatus() != null, SysMailLog::getSendStatus, bo.getSendStatus());
        lqw.between(params.get("beginSendTime") != null && params.get("endSendTime") != null,
            SysMailLog::getSendTime ,params.get("beginSendTime"), params.get("endSendTime"));
        lqw.orderByDesc(SysMailLog::getSendTime);
        return lqw;
    }

    /**
     * 新增邮件日志
     */
    @Override
    public Boolean insertByBo(SysMailLogBo bo) {
        SysMailLog add = MapstructUtils.convert(bo, SysMailLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改邮件日志
     */
    @Override
    public Boolean updateByBo(SysMailLogBo bo) {
        SysMailLog update = MapstructUtils.convert(bo, SysMailLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysMailLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除邮件日志
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
